package collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 数组转换为集合
 * 数组的工具类Arrays提供了一个静态方法asList,可以将一个数组转换为一个list集合*/
public class ArrayToListDemo {
    public static void main(String[] args) {
        String[] array ={"one","two","three","four","five"};
        System.out.println("array:"+ Arrays.toString(array));
        List<String> list =Arrays.asList(array);
        System.out.println("list"+list);
        list.set(1,"six");
        System.out.println("list"+list);
        System.out.println("array"+Arrays.toString(array));
    // 会抛出异常
//        list.add("seven");
//        System.out.println("list"+list);
//        System.out.println("array:"+Arrays.toString(array));

//        List<String> list2 = new ArrayList<>();
//        list2.addAll(list);
//        System.out.println("list2:"+list2);
//        list2.add("seven");
//        System.out.println("list2+"+list2);

//因为有构造器 所以作为参数Collection构造器传入
        List<String> list2 = new ArrayList<>(list);
        System.out.println("list2:"+list2);
        list2.add("seven");
        System.out.println("list2+"+list2);
    }
}
